/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
//作者:AKPower
//算法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 1e6 + 100;
const ll INF = (1LL << 31);
pair<ll, ll> v[maxn];
ll n, k;
ll col[maxn];
int main()
{
    IOS;
    ll cnt = 0;
    cin >> n >> k;
    for (ll i = 1; i <= k; i++)
    {
        ll t, x;
        cin >> t;
        while (t--)
        {
            cin >> x;
            v[++cnt] = make_pair(x, i);
        }
    }
    sort(v + 1, v + 1 + cnt);
    ll ans = INF;
    ll l = 1, r = 1;
    ll col_cnt = 0;
    for (; r <= cnt;)
    {
        ll xi = v[r].first;
        while (v[r].first == xi)
        {
            if (col[v[r].second] == 0)
            {
                col_cnt++;
            }
            col[v[r].second]++;
            r++;
        }
        if (r > cnt)
        {
            break;
        }
        while (col_cnt == k && l < r)
        {
            ans = min(ans, v[r - 1].first - v[l].first);
            ll xi = v[l].first;
            while (v[l].first == xi)
            {
                col[v[l].second]--;
                if (col[v[l].second] == 0)
                {
                    col_cnt--;
                }
                l++;
            }
        }
    }
    while (l <= cnt && col_cnt == k)
    {
        ans = min(ans, v[cnt].first - v[l].first);
        ll xi = v[l].first;
        while (v[l].first == xi)
        {
            col[v[l].second]--;
            if (col[v[l].second] == 0)
            {
                col_cnt--;
            }
            l++;
        }
    }
    cout << ans << endl;
    getchar();
    getchar();
    return 0;
}
